package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Book;
import  dao.BookDAO;
import entity.Page;

/**
 * Servlet implementation class BookServlet
 */
@WebServlet("/BookShow")
public class BookShowServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BookDAO bookDao = new BookDAO();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookShowServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		int curPage = 1;
		int totalCount = bookDao.getTotalCount();
		
		if(request.getParameter("curPage") != null){
			curPage = Integer.parseInt(request.getParameter("curPage"));
		}else {
			Page page = new Page(1, totalCount);
		}
		
		Page page = new Page(curPage, totalCount);

		List<Book> books = bookDao.getBookByPage(page);
		request.setAttribute("page", page);
		request.setAttribute("Books", books);
		request.getRequestDispatcher("showbook.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
